<?php
/**
 *  @author wyl
 *  @date 2015-9-7 15:06:49
 */
Yii::import('application.components.cronjob.class.*');
class CronjobMGTCommand extends CConsoleCommand{
	public function run($args){
		if(!empty($args)){
			switch ($args[0]){
				case 'list':
					break;
				case 'stop':					
					$filePool = new FilePool();
					$filePool->setNamespace($args[1]);
					$filePool->set('STOP');
					break;
				case 'start':
					$start=new StartCommand();
					$start->run($args[1],array('forever',$args[2]));
					break;
			}
			if($args[0]=='list'){
				$this->plist();
			}
		}else{
			echo 'Command is invalid!';
		}	
	}
	
	public function plist(){
		$a=exec('ps ax -o pid,cmd | grep yiic | grep -v grep | grep -v CronjobMGT | grep -v cronjobMGT ',$return,$status);
		$cmds=array();
		echo "PID\tCRONJOB\n";
		foreach ($return as $ps){
			$ps=trim($ps);
			if($num=stripos($ps,' ')){
				$pid=substr($ps,0,$num);
				$cmd=substr($ps,$num+1);
				$cmds[$pid]['pid']=$pid;
				$cmds[$pid]['cmd']=$cmd;
				echo "{$pid}\t{$cmd}\n";
				
			}
		}
	}
}
